
jQuery(window).load(function() {
	
	window.babAddonWidgets.fileicons = new Array();
	window.babAddonWidgets.selfpage = null;

	// grab all file icon waiting for a thumbnail
	
	jQuery('.widget-fileicon')
	.each(function() {
		var fileicon = jQuery(this);
		
		var meta = window.babAddonWidgets.getMetadata(fileicon.attr('id'));
		
		
		if (meta && meta.pending_thumbnail)
		{
			window.babAddonWidgets.fileicons.push(fileicon.attr('id'));
			window.babAddonWidgets.selfpage = meta.selfpage;
		}
	});
	
	
	
	if (null == window.babAddonWidgets.selfpage)
	{
		return;
	}
	
	
	// query ovidentia to get thumbnails
	
	var filecount = 0;
	var nextquery = new Array();
	
	while(window.babAddonWidgets.fileicons.length > 0)
	{
		
		
		nextquery.push(window.babAddonWidgets.fileicons.shift());
		filecount++;
		
		if (filecount > 10)
		{
			window.babAddonWidgets.processThumbnails(window.babAddonWidgets.selfpage, nextquery);
			nextquery.length = 0;
			filecount = 0;
		}
	}
	
	if (nextquery.length > 0)
	{
		window.babAddonWidgets.processThumbnails(window.babAddonWidgets.selfpage, nextquery);
	}
}
);


	
/**
 * 
 */
window.babAddonWidgets.processThumbnails = function(selfpage, arr)
{
	var ids = arr.join(',');
	
	jQuery.ajax({
		async: false,
	   url: selfpage,
	   data: 'tg=addon/widgets/thumbnail&ids='+ids,
	   success: function(response){
		
			if ('' == response)
			{
				return;
			}
		
			eval('var arr = '+response);
			

			for(var i = 0; i < arr.length; i++)
			{
				icon = jQuery('#'+arr[i].id);
				icon.css('background-image', 'url('+arr[i].url+')');
			}
		}
	});
}